package com.ruoyi.project.modules.userDataAuth.service.impl;

import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.project.modules.interfaceinfo.vo.InterfaceUser;
import com.ruoyi.project.modules.userDataAuth.domain.UserDataAuth;
import com.ruoyi.project.modules.userDataAuth.mapper.UserDataAuthMapper;
import com.ruoyi.project.modules.userDataAuth.service.IUserDataAuthService;
import com.ruoyi.project.modules.userDataAuth.vo.DataAuthUserInfo;
import com.ruoyi.project.modules.userDataAuth.vo.UserDataSummary;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户数据权限Service业务层处理
 *
 * @author smallrain
 * @date 2021-11-09
 */
@Service
public class UserDataAuthServiceImpl implements IUserDataAuthService {
    @Resource
    private UserDataAuthMapper userDataAuthMapper;

    /**
     * 查询用户数据权限
     *
     * @param id 用户数据权限主键
     * @return 用户数据权限
     */
    @Override
    public UserDataAuth selectUserDataAuthById(Long id) {
        return userDataAuthMapper.selectUserDataAuthById(id);
    }

    /**
     * 查询用户数据权限列表
     *
     * @param userDataAuth 用户数据权限
     * @return 用户数据权限
     */
    @Override
    public List<UserDataAuth> selectUserDataAuthList(UserDataAuth userDataAuth) {
        return userDataAuthMapper.selectUserDataAuthList(userDataAuth);
    }

    @Override
    public List<UserDataAuth> selectUserDataAuthList2(UserDataAuth userDataAuth) {
        return userDataAuthMapper.selectUserDataAuthList2(userDataAuth);
    }

    @Override
    public List<UserDataAuth> unSelectObjectList(UserDataAuth userDataAuth) {
        return userDataAuthMapper.unSelectObjectList(userDataAuth);
    }

    @Override
    public int cancelAll(String ids) {
        Long[] idArr = Convert.toLongArray(ids);
        return userDataAuthMapper.deleteUserDataAuthByIds(idArr);
    }

    @Override
    public int insertDataAuthUsers(String objectType, Long userId, String objectIds) {
        List<UserDataAuth> userDataAuths = new ArrayList<>();
        Long[] idArr = Convert.toLongArray(objectIds);
        for (Long aLong : idArr) {
            UserDataAuth userDataAuth = new UserDataAuth();
            userDataAuth.setObjectType(objectType);
            userDataAuth.setObjectId(aLong);
            userDataAuth.setUserId(userId);
            userDataAuths.add(userDataAuth);
        }
        return this.userDataAuthMapper.saveBatch(userDataAuths);
    }

    @Override
    public List<InterfaceUser> selectInterfaceUser(Long objectId, String objectType) {
        return this.userDataAuthMapper.selectInterfaceUser(objectId, objectType);
    }


    /**
     * 新增用户数据权限
     *
     * @param userDataAuth 用户数据权限
     * @return 结果
     */
    @Override
    public int insertUserDataAuth(UserDataAuth userDataAuth) {
        return userDataAuthMapper.insertUserDataAuth(userDataAuth);
    }

    /**
     * 修改用户数据权限
     *
     * @param userDataAuth 用户数据权限
     * @return 结果
     */
    @Override
    public int updateUserDataAuth(UserDataAuth userDataAuth) {
        return userDataAuthMapper.updateUserDataAuth(userDataAuth);
    }

    /**
     * 批量删除用户数据权限
     *
     * @param ids 需要删除的用户数据权限主键
     * @return 结果
     */
    @Override
    public int deleteUserDataAuthByIds(String ids) {
        return userDataAuthMapper.deleteUserDataAuthByIds(Convert.toLongArray(ids));
    }

    /**
     * 删除用户数据权限信息
     *
     * @param id 用户数据权限主键
     * @return 结果
     */
    @Override
    public int deleteUserDataAuthById(Long id) {
        return userDataAuthMapper.deleteUserDataAuthById(id);
    }


    @Override
    public List<DataAuthUserInfo> selectDataAuthUserListByObjectType(String objectType) {
        return this.userDataAuthMapper.selectDataAuthUserListByObjectType(objectType);
    }

    @Override
    public List<DataAuthUserInfo> selectUserAndDataSummaryList(String objectType) {
        UserDataAuth userDataAuth = new UserDataAuth();
        userDataAuth.setObjectType(objectType);
        List<SysRole> userRoles = ShiroUtils.getSysUser().getRoles();
        if (CollectionUtils.isNotEmpty(userRoles)) {
            List<SysRole> filterList = userRoles.stream().filter(a -> a.isAdmin()
                    || a.isArchitect()).collect(Collectors.toList());
            if (filterList == null || filterList.size() == 0) {
                userDataAuth.setUserId(ShiroUtils.getUserId());
            }
        }
        List<DataAuthUserInfo> list = this.userDataAuthMapper.selectDataAuthUserListByParams(userDataAuth);
        if(null == list || list.size() == 0) {
            return null;
        }
        // 用户开发权限数据汇总
        list.parallelStream().forEachOrdered(a -> {
            UserDataAuth uda = new UserDataAuth();
            uda.setUserId(a.getUserId());
            uda.setObjectType(objectType);
            List<UserDataAuth> objectList = userDataAuthMapper.selectDataListByParams(uda);
            if (null == objectList || objectList.size() == 0) {
                return;
            }
            int totalCount = objectList.size();
            a.setDataTotalCount(totalCount);
            List<UserDataAuth> finishList = objectList.stream()
                    .filter(b -> b.getObjectStatus() == 2)
                    .collect(Collectors.toList());
            int finishCount = 0;
            if(null != finishList && finishList.size() > 0) {
                finishCount = finishList.size();
            }
            a.setDataFinishCount(finishCount);
            // 已完成2，未完成1
            a.setStatus(totalCount == finishCount ? 2 : 1);
        });
        return list;
    }

    @Override
    public List<UserDataAuth> selectDataListByParams(UserDataAuth userDataAuth) {
        return this.userDataAuthMapper.selectDataListByParams(userDataAuth);
    }

    @Override
    public void updateStatus(UserDataAuth userDataAuth) {
        this.userDataAuthMapper.updateStatus(userDataAuth);
    }
}
